package com.ldfile.server.controller;

import com.ldfile.server.dto.BaseFileCompanyVO;
import com.ldfile.server.dto.BaseFileDTO;
import com.ldfile.server.response.SimpleResponse;
import com.ldfile.server.service.impl.ZipfileTask;
import com.ldfile.server.utils.ZipUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author callmecool.ldsoft
 * @description tools
 * @since 2020/7/30 15:31
 */
@RequestMapping("/api")
@Controller
@Slf4j
public class FileDownloadController {

    ExecutorService executorService = Executors.newSingleThreadExecutor();

    /**
     * 文件打包下载
     */
    @PostMapping("/downloads")
    public void fileDownloads(@RequestBody List<BaseFileDTO> filePaths, HttpServletResponse response) {
        log.debug("请求进入");
        ZipUtil.downloadZip(filePaths, response);
        log.debug("请求完成");
    }

    /**
     * 文件打包下载
     */
    @PostMapping("/zipFile")
    @ResponseBody
    public SimpleResponse zipFile(@RequestBody BaseFileCompanyVO zipInfo) {
        log.debug("请求进入");
        executorService.submit(new ZipfileTask(zipInfo));
        log.debug("请求完成");
        return SimpleResponse.success("成功");
    }

}
